package com.tcxhb.job.core.service.job.impl;

import com.tcxhb.job.dao.dataobject.JobMachineDO;
import com.tcxhb.job.dao.dataobject.query.JobMachineQuery;
import com.tcxhb.job.dao.repository.JobMachineRepository;
import com.tcxhb.job.common.utils.LocalCache;
import com.tcxhb.job.core.constants.enums.OnlineStatus;
import com.tcxhb.job.core.service.job.JobMachineService;
import com.tcxhb.job.common.model.PageResponse;
import com.tcxhb.job.common.utils.ParamUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * author:auto.generator
 * time: 2023-11-05
 */
@Service
public class JobMachineServiceImpl implements JobMachineService {
    @Resource
    private JobMachineRepository repository;
    private LocalCache<List<JobMachineDO>> cache = new LocalCache<List<JobMachineDO>>("mc");
    @Override
    public Long create(JobMachineDO machineDO) {
        JobMachineDO exit = queryByHost(machineDO.getIp(), machineDO.getPort(), machineDO.getAppName());
        if (exit == null) {
            return repository.create(machineDO);
        }
        machineDO.setId(exit.getId());
        Boolean result = repository.updateById(machineDO);
        return exit.getId();
    }

    @Override
    public boolean deleteById(Long id) {
        ParamUtils.notNull(id, "id不能为空");
        return repository.deleteById(id);
    }

    @Override
    public boolean updateById(JobMachineDO tcJobMachine) {
        return repository.updateById(tcJobMachine);
    }

    @Override
    public JobMachineDO queryById(Long id) {
        ParamUtils.notNull(id, "id不能为空");
        return repository.queryById(id);
    }

    @Override
    public PageResponse<JobMachineDO> page(JobMachineQuery query) {
        return repository.page(query);
    }

    @Override
    public List<JobMachineDO> list(JobMachineQuery query) {
        return repository.list(query);
    }

    @Override
    public boolean updateByHost(JobMachineDO machineDO) {
        return repository.updateByHost(machineDO);
    }

    @Override
    public JobMachineDO queryByHost(String host, Integer port, String appName) {
        JobMachineQuery query = new JobMachineQuery();
        query.setIp(host);
        query.setAppName(appName);
        query.setPort(port);
        List<JobMachineDO> list = repository.list(query);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<JobMachineDO> getOnlineByCache(String appName) {
        List<JobMachineDO> result = cache.get(appName);
        if(CollectionUtils.isEmpty(result)){
            JobMachineQuery query = new JobMachineQuery();
            query.setAppName(appName);
            query.setStatus(OnlineStatus.online.getType());
            List<JobMachineDO> list = repository.list(query);
            result = list;
            cache.put(appName,list);
        }
        return result;
    }

}
